﻿using System;
using System.Web.UI;
using Emz.Core.Models;
using Legazpi.Payroll.Services.Interfaces;
using Ninject;
using Legazpi.Payroll.Web.Helpers;
using System.Web.UI.WebControls;

namespace Legazpi.Payroll.Web.Admin
{
    public partial class Clients : System.Web.UI.Page
    {
        [Inject]
        public IClientService _clientService { get; set; }
        
        protected Client Model { get; private set; }
        
        #region Implementations
        void BindDropdowns()
        {            
            //Clients/Deployed At            
            ClientDropdown.ToClientSelectList(Model.Id);
        }
        void LoadDatas()
        {
            BindDropdowns();
            BindTable();

            Id.Value = Model.Id.ToString();
            ClientCode.Value = Model.Code;
            FirmName.Value = Model.FirmName;
            Contact.Value = Model.ContactNo;
            Address.Value = Model.Address;
            RegOT.Value = Model.RegularOverTime.ToString();
            SpecialOT.Value = Model.SpecialOverTime.ToString();
            LegalHoliday.Value = Model.LegalHoliday.ToString();
            SpecialHoliday.Value = Model.SpecialHoliday.ToString();
            NightDiff.Value = Model.NightDiff.ToString();
        }
        void BindTable()
        {
            repeater.DataSource = Model.Detachments;
            repeater.DataBind();
        }
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            literalNotification.Visible = false;
            if (!Page.IsPostBack)
            {       
                int id;
                if (!string.IsNullOrEmpty(Request.QueryString["id"]))
                {
                    
                    int.TryParse(Request.QueryString["id"], out id);
                    if (id > 0)
                        //Update Data
                        Model = _clientService.GetById(id);
                }
                //New Data
                Model = Model ?? new Client { Id = 0 };                
                LoadDatas();
            }

        }


        protected void Submit_Click(object sender, EventArgs e)
        {
            string strNotification = "<i><font style='color:red'>Record Successfully {0}</font></i>";
            int id;
            if (!string.IsNullOrEmpty(Id.Value))
            {

                int.TryParse(Id.Value, out id);
                if (id > 0)                    
                    Model = _clientService.GetById(id);
                else
                    Model = new Client();
            }
            
            Model.Code = ClientCode.Value;
            Model.FirmName = FirmName.Value;
            Model.ContactNo = Contact.Value;
            Model.Address = Address.Value;
            Model.RegularOverTime =  RegOT.Value.ToDouble();
            Model.SpecialOverTime = SpecialOT.Value.ToDouble();
            Model.LegalHoliday = LegalHoliday.Value.ToDouble();
            Model.SpecialHoliday = SpecialHoliday.Value.ToDouble();
            Model.NightDiff = NightDiff.Value.ToDouble();


            if (Model.Id > 0)
            {
                _clientService.Update(Model);
                strNotification = string.Format(strNotification, "Updated!");
            }
            else 
            {
                _clientService.Add(Model);
                strNotification = string.Format(strNotification, "Added!");
            }
                
            var status = _clientService.Save();

            literalNotification.Visible = status;
            literalNotification.Text = strNotification;                           

            LoadDatas();
        }

        protected void ClientDropdown_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id;
            var dd = (DropDownList)sender;
            if (!string.IsNullOrEmpty(dd.SelectedValue))
            {

                int.TryParse(dd.SelectedValue, out id);
                if (id > 0)
                    Model = _clientService.GetById(id);
                else
                    Model = new Client();
            }
            LoadDatas();
        }
    }
}